package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.adapter.RadioAdapter;
import com.fanhao.dllo.soundfm.entity.RadioStationResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

/**
 * Created by dllo on 16/1/11.
 * 电台界面
 */
public class RadioStationFragment extends BaseFragment implements View.OnClickListener {
    private RecyclerView radioRv;
    private RadioAdapter radioAdapter;
    private RecyclerViewHeader radioRvheader;
    private LinearLayout linearLocation, linearChina, linearInternet;
    private RadioStationResult radioStationResult;
    private String url = "http://live.ximalaya.com/live-web/v1/getHomePageRadiosList?device=android";

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_radiostation;
    }

    @Override
    protected void initView() {
        radioRv = findView(R.id.rv_radioStation);
        radioRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        radioRvheader = findView(R.id.rvHeader_radioStation);
        radioAdapter = new RadioAdapter(getActivity());
        radioRv.setAdapter(radioAdapter);
        radioRvheader.attachTo(radioRv, true);

        linearLocation = findView(R.id.linear_location_radio);
        linearChina = findView(R.id.linear_china_radio);
        linearInternet = findView(R.id.linear_internet_radio);
        linearLocation.setOnClickListener(this);
        linearChina.setOnClickListener(this);
        linearInternet.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        RadioStationResult rs = gson.fromJson(response,
                                RadioStationResult.class);
                        if (rs != null) {
                            RadioStationResult.ResultEntity resultEntity = rs.getResult();
                            if (resultEntity != null) {
                                radioAdapter.addRadioData(resultEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linear_location_radio:
                goToNextPlayList(new RadioListFragment(0));
                break;
            case R.id.linear_china_radio:
                goToNextPlayList(new RadioListFragment(1));
                break;
            case R.id.linear_internet_radio:
                goToNextPlayList(new RadioListFragment(2));
                break;
        }

    }
}
